package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.disperseGold.DisperseGoldRecord;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DisperseGoldRecordDaoImpl extends TempDataObjectDao<DisperseGoldRecord> {

    public Map<Long, DisperseGoldRecord> getDisperseGoldRecordMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_disperse_gold_record where userId=?";
        Map<Long, DisperseGoldRecord> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                DisperseGoldRecord data = parse(rs);
                dataMap.put(data.getServerId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(DisperseGoldRecord data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_disperse_gold_record(userId,disperseId,serverId,detailMap) values(?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getDisperseId());
            call.setLong(i++, data.getServerId());
            call.setString(i++, JSON.toJSONString(data.getDetailMap()));
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(DisperseGoldRecord data) {
        PreparedStatement call = null;
        String sql = "update t_u_disperse_gold_record SET disperseId=?,detailMap=? where userId=? and serverId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getDisperseId());
            call.setString(i++, JSON.toJSONString(data.getDetailMap()));
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getServerId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected DisperseGoldRecord parseData(ResultSet rs) throws SQLException {
        DisperseGoldRecord data = new DisperseGoldRecord();
        data.setUserId(rs.getLong("userId"));
        data.setDisperseId(rs.getLong("disperseId"));
        data.setServerId(rs.getLong("serverId"));
        String retMap = rs.getString("detailMap");
        if (!StringUtils.isNullOrEmpty(retMap)) {
            data.setDetailMap(JSON.parseObject(retMap, new TypeReference<ConcurrentHashMap<Long, Integer>>() {}));
        }
        return data;
    }
}
